<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">上货单号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入上货单号"/>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="orderReceiveTable" lay-filter="orderReceiveTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="orderReceiveTableBar">
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="orderReceiveForm">
    <form lay-filter="orderReceiveForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">创建者</label>
            <div class="layui-input-block">
                <input name="creator" placeholder="请输入创建者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改者</label>
            <div class="layui-input-block">
                <input name="modifier" placeholder="请输入修改者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-block">
                <input name="creatorName" placeholder="请输入创建人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改人</label>
            <div class="layui-input-block">
                <input name="modifierName" placeholder="请输入修改人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配货订单编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入配货订单编号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                        <option value="created">已创建</option>
                        <option value="loading">上货中</option>
                        <option value="finish">上货完成</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配货需求单id</label>
            <div class="layui-input-block">
                <input name="demandId" placeholder="请输入配货需求单id" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderReceiveFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="detailTab">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <table class="layui-table" id="orderReceiveTable4Detail" lay-filter="orderReceiveTable4Detail"></table>
                <br>
                <!-- 表格 -->
                <table class="layui-table" id="orderReceiveDetailTable" lay-filter="orderReceiveDetailTable"></table>
            </div>
        </div>
    </div>

</script>

<script type="text/html" id="orderReceiveDetailTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="labelDetail">条码列表</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="loadDetail">上货详情</a>
</script>


<script type="text/html" id="loadTable">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->

                <!-- 表格 -->
                <table class="layui-table" id="orderReceiveLoadTable" lay-filter="orderReceiveLoadTable"></table>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="labelTable">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->

                <!-- 表格 -->
                <table class="layui-table" id="orderReceiveLabelTable" lay-filter="orderReceiveLabelTable"></table>
            </div>
        </div>
    </div>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#orderReceiveTable',
            url: config.base + 'enterprisecab/order/admin/orderLoadTask/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}

                ,{field: 'taskCode', sort: true, title: '上货任务单号'}
                ,{field: 'skuQuantity', sort: true, title: '物料数量'}
                ,{field: 'statusDesc', sort: true, title: '状态'}
                ,{field: 'createTime', sort: true, title: '提交时间'}
                ,{field: 'creatorName', sort: true, title: '创建人'}
                ,{align: 'center', toolbar: '#orderReceiveTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(orderReceiveTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if(obj.event=='detail'){
                showDetailModel(data);
            }
        });

        //监听排序
        table.on('sort(orderReceiveTable)', function(obj){
            table.reload('orderReceiveTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#orderReceiveBtnAdd').click(function () {
            showEditModel();
        });

        function getReceiveTable(id){
            var insTb = table.render({
                elem: '#orderReceiveTable4Detail',
                url: config.base + 'enterprisecab/order/admin/orderLoadTask/list?id='+id,
                page: false,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}

                    ,{field: 'taskCode', sort: true, title: '上货任务编号'}
                    ,{field: 'skuQuantity', sort: true, title: '物料数量'}
                    ,{field: 'warehouseName', sort: true, title: '仓库'}
                    ,{field: 'statusDesc', sort: true, title: '状态'}
                    ,{field: 'createTime', sort: true, title: '提交时间'}
                    ,{field: 'creatorName', sort: true, title: '创建人'}
                    // ,{align: 'center', toolbar: '#orderReceiveTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });
        }

        function getDetailTable(receiveId){
            // 渲染表格
            var insTb = table.render({
                elem: '#orderReceiveDetailTable',
                url: config.base + 'enterprisecab/order/admin/orderReceiveDetail/list?receiveId='+receiveId,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}

                    ,{field: 'skuCode', sort: true, title: '物料编号'}
                    ,{field: 'skuName', sort: true, title: '物料名称'}
                    ,{field: 'skuSpec', sort: true, title: '物料规格'}
                    ,{field: 'productBrandName', sort: true, title: '品牌'}
                    ,{field: 'unitName', sort: true, title: '物料单位'}
                    ,{field: 'marketPrice', sort: true, title: '物料单价'}
                    ,{field: 'quantityConfirm', sort: true, title: '需求数量'}
                    ,{field: 'quantityReceive', sort: true, title: '领取数量'}
                    ,{field: 'creatorName', sort: true, title: '领取人'}
                    ,{field: 'createTime', sort: true, title: '领取时间'}
                    ,{align: 'center', toolbar: '#orderReceiveDetailTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });

            // 工具条点击事件
            table.on('tool(orderReceiveDetailTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }else if(obj.event=='loadDetail'){
                    showLoadModel(data);
                }else if(obj.event=='labelDetail'){
                    showLabelModel(data);
                }
            });

            //监听排序
            table.on('sort(orderReceiveDetailTable)', function(obj){
                table.reload('orderReceiveDetailTable', {
                    initSort: obj
                    ,where: {
                        sort: obj.field
                        ,order: obj.type
                    }
                });
            });
        }

        function getLoadTable(receiveDetailId){
            // 渲染表格
            var insTb = table.render({
                elem: '#orderReceiveLoadTable',
                url: config.base + 'enterprisecab/order/admin/orderReceiveLoad/list?receiveDetailId='+receiveDetailId,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}
                    ,{field: 'skuCode', sort: true, title: '物料编码'}
                    ,{field: 'skuName', sort: true, title: '物料名称'}
                    ,{field: 'skuSpec', sort: true, title: '物料规格'}
                    ,{field: 'productBrandName', sort: true, title: '品牌'}
                    ,{field: 'unitName', sort: true, title: '物料单位'}
                    ,{field: 'cabCode', sort: true, title: '柜子编号'}
                    ,{field: 'spaceCode', sort: true, title: '货道编号'}
                    ,{field: 'quantity', sort: true, title: '上货数量'}
                    ,{field: 'creatorName', sort: true, title: '上货人'}
                    ,{field: 'createTime', sort: true, title: '上货时间'}
                    ,{align: 'center', toolbar: '#orderReceiveLoadTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });

            // 工具条点击事件
            table.on('tool(orderReceiveLoadTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }
            });

            //监听排序
            table.on('sort(orderReceiveLoadTable)', function(obj){
                table.reload('orderReceiveLoadTable', {
                    initSort: obj
                    ,where: {
                        sort: obj.field
                        ,order: obj.type
                    }
                });
            });

            // 搜索
            form.on('submit(formSubSearchLog)', function (data) {
                insTb.reload({where: data.field,page: {curr: 1}},'data');
            });
        }

        function getLabelTable(receiveDetailId){
            // 渲染表格
            var insTb = table.render({
                elem: '#orderReceiveLabelTable',
                url: config.base + 'enterprisecab/order/admin/orderReceiveLabel/list?receiveDetailId='+receiveDetailId,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'checkbox'}
                    ,{field: 'packLabelCode', sort: true, title: '包装编码'}
                    ,{field: 'quantity', sort: true, title: '数量'}

                    ,{field: 'packName', sort: true, title: '包装规格名称'}
                    ,{field: 'skuCode', sort: true, title: '物料编码'}
                    ,{field: 'skuName', sort: true, title: '物料名称'}
                    ,{field: 'skuSpec', sort: true, title: '物料规格'}
                    ,{field: 'productBrandName', sort: true, title: '品牌'}
                    ,{field: 'unitName', sort: true, title: '物料单位'}
                    // ,{field: 'warehouseId', sort: true, title: '仓库id'}
                    // ,{field: 'areaId', sort: true, title: '库区id'}
                    // ,{field: 'shelfId', sort: true, title: '货架id'}
                    // ,{field: 'spaceId', sort: true, title: '货位id'}
                    // ,{field: 'boxId', sort: true, title: '料箱id'}
                    ,{field: 'creatorName', sort: true, title: '领取人'}
                    ,{field: 'createTime', sort: true, title: '领取时间'}
                    // ,{align: 'center', toolbar: '#orderReceiveLabelTableBar', title: '操作', width: 200}
                ]],
                done: function (res, curr, count) {
                    //移除按钮
                    var menuButton = config.getMenuButton(location.hash);
                    if(menuButton.indexOf("搜索") == -1){
                        $('.layui-form.toolbar').remove();
                    }
                    if(menuButton.indexOf("修改") == -1){
                        $("[lay-event='edit']").remove();
                    }
                    if(menuButton.indexOf("删除") == -1){
                        $("[lay-event='del']").remove();
                    }
                }
            });

            // 工具条点击事件
            table.on('tool(orderReceiveLabelTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') { //修改
                    showEditModel(data);
                } else if (obj.event === 'del') { // 删除
                    doDelete(obj);
                }
            });

            //监听排序
            table.on('sort(orderReceiveLabelTable)', function(obj){
                table.reload('orderReceiveLabelTable', {
                    initSort: obj
                    ,where: {
                        sort: obj.field
                        ,order: obj.type
                    }
                });
            });
        }

        function showDetailModel(data){
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '详情',
                content: $('#detailTab').html(),
                success: function () {
                    getReceiveTable(data.id);
                    getDetailTable(data.id);
                }
            })
        }

        function showLoadModel(data){
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '上货列表',
                content: $('#loadTable').html(),
                success: function () {
                    getLoadTable(data.id);
                }
            })
        }

        function showLabelModel(data){
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '条码列表',
                content: $('#labelTable').html(),
                success: function () {
                    getLabelTable(data.id);
                }
            })
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '640px',
                offset: '65px',
                title: data ? '修改领货单' : '添加领货单',
                content: $('#orderReceiveForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    form.val('orderReceiveForm', data);
                    // 表单提交事件
                    form.on('submit(orderReceiveFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprisecab/order/admin/orderReceive/update' : 'enterprisecab/order/admin/orderReceive/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderReceiveTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/order/admin/orderReceive/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#orderReceiveBtnExport').click(function () {
            var checkRows = table.checkStatus('orderReceiveTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>